/* eslint-disable no-param-reassign */
/* eslint-disable no-undef */
/* eslint-disable no-shadow */

const config = {
  baseUrl: 'http://192.168.3.21/go',
  dataType: 'json',
  showLoading: false,
  loadingText: '请求中...',
  loadingTime: 1000,
  originalData: true,
  loadingMask: true,
  header: { 'content-type': 'application/json;charset=UTF-8' },
};

const install = (Vue, vm) => {
  Vue.prototype.$u.http.setConfig(config);

  // 请求拦截部分，每次请求前都会执行
  Vue.prototype.$u.http.interceptor.request = (config) => {
    config.header.Authorization = vm.vuex_token;

    return config;
  };

  Vue.prototype.$u.http.interceptor.response = (raw) => {
    const { statusCode, data, errMsg } = raw;

    if (errMsg !== 'request:ok') {
      // Vue.prototype.$u.toast(errMsg);
      
      return false;
    }

    const { code, msg } = data;

    if (statusCode !== 200) {
      Vue.prototype.$u.toast(data);
      return false;
    }

    if (code === 0) {
      return data;
    }

    Vue.prototype.$u.toast(msg);
    return false;
  };
};

export default {
  install,
};
